<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="hardcode2013.*" %>

<html>
	<head>
	    <link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
	    
		<script type="text/javascript">
		    function selectAllOnChange(checkbox) {
				   var selectCheckboxes = document.getElementsByName("select_checkbox");
				   for (var i = 0; i < selectCheckboxes.length; i++) {
					   selectCheckboxes[i].checked = checkbox.checked;
				   }
		 	}
		    function itemAction(selValue, keyValue) {
			   if (selValue != null) {
				document.getElementById("action").value = "single_item_action"; 
		    	document.getElementById("single_item_action").value = selValue;
		    	document.getElementById("single_item_key").value = keyValue;
		    	document.getElementById("item_form").submit();   
		 		 }
		      }
		</script>
	</head>

	<body>
	
  	<%@include file="include_header.jsp" %>
  	
<fieldset>
	<legend>Item List</legend>
	<%
	final String currentUrl = "/index.jsp";
	
	String pageStr = request.getParameter("page");
	int currentPage = 1;
	if (pageStr != null) {
		try {
			currentPage = Integer.parseInt(pageStr);
		} catch (Exception e) {
		}
	}
	
	if (currentPage <= 0) {response.sendRedirect(currentUrl); return;}
	ItemServ.ItemQueryResult itemRes = ItemServ.getAllItems(currentPage, (profile != null && profile.getIsAdmin()), (profile != null ? profile.getUsername() : null));
	int totalPages = itemRes.totalItems / ItemServ.resultsPerPage + (itemRes.totalItems % ItemServ.resultsPerPage == 0 ? 0 : 1);
	if (currentPage > totalPages && itemRes.totalItems > 0) {response.sendRedirect(currentUrl + "?page=" + totalPages); return;}
	
	String pageLbl = "Total Items: " + itemRes.totalItems;
	if (itemRes.totalItems > 0)
		pageLbl += ", " +
		"Showing: " + ((currentPage - 1) * ItemServ.resultsPerPage + 1) + "-" + (currentPage * ItemServ.resultsPerPage > itemRes.totalItems ? itemRes.totalItems : currentPage * ItemServ.resultsPerPage) + ", " +
		"Page: " + currentPage + " / " + totalPages +
		(currentPage > 1 ? " <a href=\"" + EncodeUtil.encodeHtml(currentUrl) + "?page=" + (currentPage - 1) + "\">Prev</a>" : "") +
		(currentPage < totalPages ? " <a href=\"" + EncodeUtil.encodeHtml(currentUrl) + "?page=" + (currentPage + 1) + "\">Next</a>" : "");
	%>
	
	<%= pageLbl %>
	
    <form id="item_form" action="/item" method="post">
	    <table border="1" width="100%">
	    <tr>
	    	<%
	    		if (profile != null && profile.getIsAdmin()) {
	    	%><td><input type="checkbox" onchange="selectAllOnChange(this)"></td><%
	    		}
	    	%>
		    <td>Title</td>
	  	    <td>Description</td>
	   	    <td>Price</td>
	   	    <td>Creation Date</td>
	   	    <td>Expiration Date</td>
	   	    <td>Seller</td>
	   	    <td>Action</td>
	    </tr>
	<%
		for (ItemProfile i : itemRes.itemList) {
	%>
		<tr>
			<%
				if (profile != null && profile.getIsAdmin()) {
			%><td><input type="checkbox" name="select_checkbox" value=<%= EncodeUtil.encodeHtml(i.getKeyStr()) %>></td><%
				}
			%>
		    <td><%=EncodeUtil.encodeHtml(i.getTitle())%></td>
	  	    <td><%=EncodeUtil.encodeHtml(i.getDescription()).replaceAll("\n", "<br>")%></td>
	   	    <td>$<%=EncodeUtil.encodeHtml(i.getPrice())%></td>
	   	    <td><%=EncodeUtil.encodeHtml(i.getCreationDateTime())%></td>
	   	    <td><%=EncodeUtil.encodeHtml(i.getExpirationDateTime())%></td>
	   	    <td><%=EncodeUtil.encodeHtml(i.getSeller())%></td>
	   	    <td>
				<% if (profile != null) { %>
					<select onchange="itemAction(this.options[this.selectedIndex].value, <%= EncodeUtil.encodeHtml("\"" + i.getKeyStr() + "\"") %>)">
						<option value=null>Select Action</option>
						<% if (! ItemServ.matchSellerUser(i.getKeyStr(), profile.getUsername())) { %>
							<option value="mail">Mail</option>
						<% } %>
					  	<% if (profile.getIsAdmin() || ItemServ.matchSellerUser(i.getKeyStr(), profile.getUsername())) { %>
						  	<option value="edit">Edit</option>
					  	<% } %>
					  	<% if (profile.getIsAdmin() || ItemServ.matchSellerUser(i.getKeyStr(), profile.getUsername())) { %>
							<option value="delete">Delete</option>
					  	<% } %>
					</select>
				<% } %>
			</td>
	    </tr>
		<%
	}
	%>
	
	</table>
	<select name="multi_item_action" style="display: <%= (profile != null && profile.getIsAdmin() ? "inline" : "none") %>">
		<option value=null>Select Action</option>
		<option value="delete">Delete</option>
	</select>
	<input type="hidden" id="single_item_action" name="single_item_action" value=null>
	<input type="hidden" id="single_item_key" name="single_item_key" value=null>
	<input type="hidden" id="action" name="action" value="multi_item_action">
	<input type="hidden" name="redirect" value="<%= "/index.jsp?page=" + String.valueOf(currentPage) %>">
	<input type="hidden" name="auth_key" value="<%= SessionUtil.getAuthKey(request) %>">
	<input type="submit" value="Perform" style="display: <%= (profile != null && profile.getIsAdmin() ? "inline" : "none") %>" />
	</form>
	</fieldset>
	</body>
</html>